export function getCookie(name) {
    let matches = document.cookie.match(new RegExp(
      "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
    ));
    return matches ? decodeURIComponent(matches[1]) : undefined;
}

export function setCookie(name, value, options = {}) {
      let defaultOptions = {
          path: '/',
      };
      options = Object.assign(defaultOptions, options);
      if (options.expires instanceof Date) {
          options.expires = options.expires.toUTCString();
      } else {
          if(options.expires) {
              options.expires = new Date(Date.now() + Number.parseInt(options.expires) * 1000);
          }
      }
      let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value);
      for (let optionKey in options) {
          updatedCookie += "; " + optionKey;
          let optionValue = options[optionKey];
          if (optionValue !== true) {
              updatedCookie += "=" + optionValue;
          }
      }
      document.cookie = updatedCookie;
}
